﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using MovieRental.SqlRepositoryLibrary.Helpers;
using MovieRental.Entities;

namespace MovieRental.SqlRepositoryLibrary.Converters
{
    public class LoanCartConverter : BaseConverter<LoanCartInfo, Guid>
    {
        protected override SqlEntityMapping SetUpMapping()
        {
            var mapping = new SqlEntityMapping("LoanCarts", "Id");
            mapping.AddProperty("Id", "Id", SqlDbType.UniqueIdentifier);
            mapping.AddProperty("CustomerId", "CustomerId", SqlDbType.UniqueIdentifier);
            mapping.AddProperty("LoanDate", "LoanDate", SqlDbType.DateTime);
            mapping.AddProperty("CartDiscountValue", "CartDiscountValue", SqlDbType.Float);
            return mapping;
        }

        protected override void SetUpPropertyValues(LoanCartInfo entity)
        {
            SetProperty("Id", entity.Id);
            SetProperty("CustomerId", entity.CustomerId);
            SetProperty("LoanDate", entity.LoanDate);
            SetProperty("CartDiscountValue", entity.CartDiscountValue);
        }

        public override LoanCartInfo ConvertFromDataReader(SqlDataReader reader)
        {
            Guid id = (Guid)GetValueForProperty(reader, "Id");
            LoanCartInfo cart = new LoanCartInfo(id);
            var customerId = GetValueForProperty(reader, "CustomerId");
            cart.CustomerId = (customerId == DBNull.Value) ? Guid.Empty : (Guid)customerId;
            cart.LoanDate = (DateTime)GetValueForProperty(reader, "LoanDate");
            cart.CartDiscountValue = (double)GetValueForProperty(reader, "CartDiscountValue");
            return cart;
        }
    }
}
